package com.xuxiang.cloud.service;

import com.xuxiang.grpc.proto.GreeterGrpc;
import com.xuxiang.grpc.proto.HelloReply;
import com.xuxiang.grpc.proto.HelloRequest;
import io.grpc.StatusRuntimeException;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * @author xuxiang 2019/5/30 22:32
 */
@Service
public class GreeterClientService {

    @GrpcClient("spring-server-consul")
    private GreeterGrpc.GreeterBlockingStub greeterBlockingStub;

    public String sayHello(final String name) {
        try {
            final HelloReply response = this.greeterBlockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());
            return response.getMessage();
        } catch (final StatusRuntimeException e) {
            return "FAILED with " + e.getStatus().getCode();
        }
    }

}
